﻿// -----------------------------------------------------------------------
// <copyright file="CachingSearchBusiness.cs" company="Matthieu Klotz">
// Copyright (c) 2012, Matthieu Klotz
// All rights reserved.
// Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
// - Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
// - Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
// - Neither the name of Matthieu Klotz nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission.
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
// </copyright>
// -----------------------------------------------------------------------

namespace AppFabric.Admin.Service.BusinessLogic
{
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text.RegularExpressions;
    using AppFabric.Admin.Service.BusinessLogic.Interfaces;
    using AppFabric.Admin.Service.DataAccess.Interfaces;
    using Microsoft.ApplicationServer.Caching;
    using Microsoft.Practices.ServiceLocation;

    /// <summary>
    /// Caching Search business class.
    /// </summary>
    public class CachingSearchBusiness : ICachingSearchBusiness, IDisposable
    {
        /// <summary>
        /// Default datacache factory.
        /// </summary>
        private DataCacheFactory dataCacheFactory;

        /// <summary>
        /// Indicates if the current instance is disposed or not.
        /// </summary>
        private bool disposed;

        /// <summary>
        /// Initializes a new instance of the <see cref="CachingSearchBusiness"/> class.
        /// </summary>
        public CachingSearchBusiness()
        {
            this.dataCacheFactory = new DataCacheFactory();
        }

        /// <summary>
        /// Finalizes an instance of the <see cref="CachingSearchBusiness"/> class.
        /// Releases unmanaged resources and performs other cleanup operations before the <see cref="CachingSearchBusiness"/> is reclaimed by garbage collection.
        /// </summary>
        ~CachingSearchBusiness()
        {
            this.Dispose(false);
        }

        /// <summary>
        /// Gets the repository.
        /// </summary>
        protected ICachingAdminDataRepository Repository
        {
            get
            {
                return ServiceLocator.Current.GetInstance<ICachingAdminDataRepository>();
            }
        }

        /// <inheritdoc />
        public IList<string> SearchCacheItems(string cacheKeyPattern, string namedCache, string region)
        {
            List<string> results = new List<string>();
            DataCache cache = this.dataCacheFactory.GetCache(namedCache);

            if (cache != null)
            {
                IList<DataAccess.Datas.Region> dataRegions = null;
                if (!string.IsNullOrWhiteSpace(region))
                {
                    dataRegions = new List<DataAccess.Datas.Region>();
                    DataAccess.Datas.Region dataRegion = new DataAccess.Datas.Region { RegionName = region };
                    dataRegions.Add(dataRegion);
                }

                if (dataRegions == null)
                {
                    dataRegions = this.Repository.GetCacheRegions(namedCache);
                }

                if (dataRegions != null && dataRegions.Count > 0)
                {
                    foreach (DataAccess.Datas.Region dataRegion in dataRegions)
                    {
                        if (region == null)
                        {
                            continue;
                        }

                        IEnumerable<KeyValuePair<string, object>> objects = cache.GetObjectsInRegion(region);
                        if (objects != null && objects.Any() && cacheKeyPattern != null)
                        {
                            Regex regex = new Regex(cacheKeyPattern, RegexOptions.CultureInvariant | RegexOptions.IgnoreCase | RegexOptions.Singleline);
                            foreach (KeyValuePair<string, object> cacheItem in objects)
                            {
                                if (regex.IsMatch(cacheItem.Key))
                                {
                                    results.Add(cacheItem.Key);
                                }
                            }
                        }
                    }
                }
            }

            return results;
        }

        /// <inheritdoc />
        public bool RemoveCacheItem(string cacheKey, string namedCache)
        {
            bool cacheItemRemoved = false;
            DataCache cache = this.dataCacheFactory.GetCache(namedCache);
            if (cache != null)
            {
                cacheItemRemoved = cache.Remove(cacheKey);
            }

            return cacheItemRemoved;
        }

        /// <summary>
        /// Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources.
        /// </summary>
        public void Dispose()
        {
            this.Dispose(true);
            GC.SuppressFinalize(this);
        }

        /// <summary>
        /// Releases unmanaged and - optionally - managed resources
        /// </summary>
        /// <param name="disposing"><c>true</c> to release both managed and unmanaged resources; <c>false</c> to release only unmanaged resources.</param>
        private void Dispose(bool disposing)
        {
            if (!this.disposed)
            {
                if (disposing)
                {
                    if (this.dataCacheFactory != null)
                    {
                        this.dataCacheFactory.Dispose();
                    }
                }

                this.dataCacheFactory = null;
                this.disposed = true;
            }
        }
    }
}
